﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;
using DotNet.Common.NHibernateUtil.QueryFilter;
using NHibernate;
using ZLCMS.Commands.User.Commands.Product;
using ZLCMS.Commands.User.Util;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Product
{
    public partial class ProductList : System.Web.UI.Page
    {
        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;

        protected string Kind { get; set; }

        public string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);

            _userAccount = UserKit.GetUserAccountFailLogin();
            //设置分页大小
            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;

            //设置页码
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;

            //绑定产品状态下拉框
            ParamKit.Bind("ProductStatus", ddl_status, null, new System.Collections.Generic.List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, Request["status"], "-1");

            string departmentCode = "";
            if (AuthUtil.CheckAuthorization("PRODUCT_MAN", "VIEW_ALL", _userAccount))
            {
            }
            else if (AuthUtil.CheckAuthorization("PRODUCT_MAN", "VIEW_OWNER", _userAccount))
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    departmentCode = session.CreateSQLQuery("select dept_code from sys_user_info where user_account=:acc")
                        .SetParameter("acc", _userAccount).UniqueResult<string>();
                }
            }
            else
            {
                //没有权限访问
                departmentCode = "no_dept";
            }

            LoadProductsResult res = new LoadProductsCommand().Execute(new LoadProductsParameter()
            {
                ProductName = Request["name"],
                ProductNo = Request["productno"],
                Status = Request["status"],
                OwnerAccount = _userAccount,
                DeptCode = departmentCode,
                Kind = Request["kind"],
                PageIndex = pageIndex,
                PageSize = pageSize
            }).Result;
            //设置数据源
            repeater1.DataSource = res.Products;
            repeater1.DataBind();
        }

        #region 显示操作项目链接 -protected string ShowLink(string ids, string status)
        /// <summary>
        /// 显示操作项目链接
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        protected string ShowLink(string pid, string status)
        {
            string retString = null;

            if ( AuthUtil.CheckAuthorization("PRODUCT_MAN", "SJXJ", _userAccount))
            {
                if (status == "0")
                {
                    retString = "<a href='/SystemManage/Basics/ProductSKU/ProductList.aspx?pid=" + pid + "&opt=drop' target='ajaxTodo' title='请确定要设置上架'>设置上架</a>&nbsp;&nbsp;";
                }
                else if (status == "1")
                {
                    retString = "<a href='/SystemManage/Basics/ProductSKU/ProductList.aspx?pid=" + pid + "&opt=show' target='ajaxTodo' title='请确定要设置下架'>设置下架</a>&nbsp;&nbsp;";
                }
            }

            return retString;
        }
        #endregion

        public string GetQrcodeUrl(object obj)
        {
            ShopProductInfo item = obj as ShopProductInfo;
            if (string.IsNullOrEmpty(item.QrcodeUrl))
            {
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    item.QrcodeUrl = QRCodeKit.RenderQrCode(ServerUtil.GetRootURI() + "/Wap/Shop/ProductDetail.aspx?no=" + item.ProductNo, item.ProductNo, 300, 300);
                    session.Update(item);
                    session.Flush();
                }
            }
            return string.Format("<a href='/Tools/DownloadFile.aspx?filepath={0}&filename={1}' target='_blank'><img src='{2}' alt='商品二维码' width='80px'/></a>", item.QrcodeUrl, item.ProductNo, item.QrcodeUrl);
        }
    }
}